import { Component, Injector, ViewChild, ViewEncapsulation, OnInit, Inject, Optional, InjectionToken } from '@angular/core';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { PrimengTableHelper } from 'shared/helpers/PrimengTableHelper';
import { Paginator } from 'primeng/components/paginator/paginator';
import { EntityDtoOfInt64, TokenAuthServiceProxy, TimeLimitListDto, TimeLimitServiceProxy } from '@shared/service-proxies/service-proxies';
import { FileUploader, FileUploaderOptions, FileItem } from 'ng2-file-upload';
import { Table } from 'primeng/components/table/table';
import { AppComponentBase } from '@shared/common/app-component-base';
import { AppConsts } from '@shared/AppConsts';
import { IAjaxResponse } from '@abp/abpHttpInterceptor';
import { TokenService } from '@abp/auth/token.service';

@Component({
  selector: 'app-time-limit',
  templateUrl: './time-limit.component.html',
  styleUrls: ['./time-limit.component.css']
})
export class TimeLimitComponent extends AppComponentBase implements OnInit {
  @ViewChild('paginator') paginator: Paginator;
  @ViewChild('dataTable') dataTable: Table;
  primengTableHelper: PrimengTableHelper;
  filterText = '';
  advancedFiltersAreShown = false;
  searchArea = '';
  searchLimitDay = 0;

  excelUploader: FileUploader;

  constructor(injector: Injector,
    private _timeLimitServiceProxy: TimeLimitServiceProxy,
    private _tokenService: TokenService,
  ) {super(injector); }

  ngOnInit(): void {

    this.initUploaders();
  }

  initUploaders(): void {
    this.excelUploader = this.createUploader(
      '/TimeLimit/ImportFromExcel?v=' + Date.now(),
      result => {
        this.appSession.tenant.logoFileType = result.fileType;
        this.appSession.tenant.logoId = result.id;
      }
    );




  }

  createUploader(url: string, success?: (result: any) => void): FileUploader {
    const uploader = new FileUploader({ url: AppConsts.remoteServiceBaseUrl + url });

    uploader.onAfterAddingFile = (file) => {
      file.withCredentials = false;
      //uploader.uploadAll();

    };

    uploader.onSuccessItem = (item, response, status) => {
      const ajaxResponse = <IAjaxResponse>JSON.parse(response);
      if (ajaxResponse.success) {
        this.notify.info(this.l('SavedSuccessfully'));
        if (success) {
          this.getPageList();
          success(ajaxResponse.result);
        }
      } else {
        this.message.error(ajaxResponse.error.message);
      }

    };

    const uploaderOptions: FileUploaderOptions = {};
    uploaderOptions.authToken = 'Bearer ' + this._tokenService.getToken();
    uploaderOptions.removeAfterUpload = true;
    uploaderOptions.autoUpload = true;
    uploaderOptions.allowedFileType = ['xlsx', 'xls'];
    uploader.setOptions(uploaderOptions);
    return uploader;
  }
  getPageList(event?: LazyLoadEvent) {
    if (this.primengTableHelper.shouldResetPaging(event)) {
      this.paginator.changePage(0);

      return;
    }

    this.primengTableHelper.showLoadingIndicator();

    this._timeLimitServiceProxy.getPageList(
      this.searchArea,
      this.searchLimitDay,
      this.primengTableHelper.getSorting(this.dataTable),
      this.primengTableHelper.getMaxResultCount(this.paginator, event),
      this.primengTableHelper.getSkipCount(this.paginator, event)
    ).subscribe(result => {
      this.primengTableHelper.totalRecordsCount = result.totalCount;
      this.primengTableHelper.records = result.items;
      this.primengTableHelper.hideLoadingIndicator();
    });
  }

}
